# coding=utf-8
# -*- coding: UTF-8 -*-
#
# This file is part of the kernelstudio package.
#
# (c) 2014-2025 zlin <admin@kernelstudio.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.


def table_to_markdown(table) -> str:
    cell_texts = [cell.text.replace("\n", "<br>") for cell in table.rows[0].cells]
    markdown_table = f"| {' | '.join(cell_texts)} |\n"
    markdown_table += f"| {' | '.join(['---'] * len(table.rows[0].cells))} |\n"

    for row in table.rows[1:]:
        # Replace newlines with <br> in each cell
        row_cells = [cell.text.replace("\n", "<br>") for cell in row.cells]
        markdown_table += "| " + " | ".join(row_cells) + " |\n"

    return markdown_table
